setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load topic model
load('models/stm10k1it.Rdata')
# the two files below are omitted from this replication package for copyright reasons
# refer to the 0_preparation/fittopicmodel.R file to see how they are generated
load('models/stmPrep.Rdata')
load('models/heldout.Rdata')

### prepare tasks
newMass <- combMass(stm10k1it)
T8WSIstm10k1it <- validateTopic(type = "T8WSI", n = 500, text = stmPrep$meta$post_text[-heldout$missing$index],
                             vocab = newMass[[1]], beta = newMass[[2]], theta = stm10k1it$theta[-heldout$missing$index,], thres = 20)
goldT8WSI <- read.csv("goldstandard/goldT8WSI.csv", stringsAsFactors = FALSE)
T8WSIstm10k1it <- mixGold(tasks = T8WSIstm10k1it, golds = goldT8WSI)
T8WSIstm10k1it <- recordTasks(type = "T8WSI", tasks = T8WSIstm10k1it, path = "topicrecords/T8WSIstm10k1it.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# T8WSIstm10k1itids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "T8WSI",
#                             tasksrecord = T8WSIstm10k1it, HITidspath = "HITids/T8WSIstm10k1itids1.Rdata")
# ### extend HITs if needed
# for(i in T8WSIstm10k1itids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*48)
# }
# 
# load("HITids/T8WSIstm10k1itids1.Rdata")
# T8WSIstm10k1itresults1 <- getResults(batch_id = "T8WSIstm10k1it1", hit_ids = HITids, retry = FALSE)
# write.csv(T8WSIstm10k1itresults1, "../../1_replication/1_topicvalidation/topicresults/T8WSIstm10k1itresults1.csv", row.names = FALSE)
# evalResults(results = T8WSIstm10k1itresults1, key = T8WSIstm10k1it, type = "T8WSI")
# 
# ### send tasks (second trial)
# T8WSIstm10k1itids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "T8WSI",
#                             tasksrecord = T8WSIstm10k1it, HITidspath = "HITids/T8WSIstm10k1itids2.Rdata")
# ### extend HITs if needed
# for(i in T8WSIstm10k1itids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*48)
# }
# 
# load("HITids/T8WSIstm10k1itids2.Rdata")
# T8WSIstm10k1itresults2 <- getResults(batch_id = "T8WSIstm10k1it2", hit_ids = HITids, retry = FALSE)
# write.csv(T8WSIstm10k1itresults2, "../../1_replication/1_topicvalidation/topicresults/T8WSIstm10k1itresults2.csv", row.names = FALSE)
# evalResults(results = T8WSIstm10k1itresults2, key = T8WSIstm10k1it, type = "T8WSI")